import sys
import os
import tempfile

from distutils.core import setup
from distutils import sysconfig


#-----------------------------------------------------------------------------
def DeleteFile( file ):
    """Delete a file"""
    try:
        os.remove( file )
    except:
        pass

#-----------------------------------------------------------------------------
if __name__ == '__main__' :

    #   Setup parameters
    dataFiles = ( 
        "surveyor.code", 
        "surveyor.examples", 
        "LICENSE", 
        "README", 
        )
    longDescription = \
"""Surveyor is a software project artifact measurment tool. It is an easy
to extend framework for producing measures (for example, various 
size or complexity measures) of files."""

    installDir = sysconfig.get_python_lib() + os.sep + 'surveyor'
    surveyor = installDir + os.sep + 'surveyor.py'
    pythonExe = sys.executable

    #   Generate script to run Surveyor for this platform
    scriptSuffix = ''
    scriptStr = '#! /bin/sh\n\n%s %s "$@"\n' % ( pythonExe, surveyor )
    if sys.platform == 'win32' :
        args = '%*'
        scriptStr = '@%s %s %s\n' % ( pythonExe, surveyor, args )
        scriptSuffix = '.bat'

    #   Create name of the script file to launch Surveyor
    scriptFile = os.path.join(
        tempfile.gettempdir(), 
        'surveyor' + scriptSuffix
        )

    #   Delete temporary file (if it already exists)
    DeleteFile( scriptFile )
    
    #   Write the script file
    try :
        file = open( scriptFile, "w" )
        file.write( scriptStr )
        file.close()
        if sys.platform != 'mac' :
            os.chmod( scriptFile, 0755 )
    except :
        print "Unable to create surveyor script."
        raise

    #   Run the install
    setup( 
        name = "Code Surveyor",
        version = "3.0",
        license = "GPL",
        author = "Matt Peloquin",
        author_email = "mattp@construx.com",
        url = "http://code.google.com/p/codesurveyor/",
        packages = [ "surveyor", "Surveyor.Modules" ],
        scripts = [ scriptFile ],
        data_files = [ ( installDir, dataFiles ) ],
        long_description = longDescription
        )

    #   Cleanup temporary script file
    DeleteFile( scriptFile )

